/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package subgradient;

/**
 * Item for the knapsack problem.
 * <p>
 * an item have a weight and a utility.
 * 
 * @author Guillaume
 */
public class Item implements Comparable{
    private final double weight;
    private final double profit;
    
    
    /**
     * Construct an item with profit and weight.
     * 
     * @param profit the profit of item
     * @param weight the weight of item
     */
    public Item(double profit, double weight){
        this.profit = profit;
        this.weight = weight;
    }
    
    public double getWeight(){
        return weight;
    }
    
    public double getProfit(){
        return profit;
    }
    
    @Override
    public int compareTo(Object o) {
        Item node = (Item) o;
            try{
                if ((this.profit/this.weight > node.profit/node.weight) ||
                                    (this.profit/this.weight == node.profit/node.weight && this.profit>node.profit)){
                    return -1;
                }else if (this.profit/this.weight == node.profit/node.weight && this.profit == this.weight){
                    return 0;
                }else{
                    return 1;
                }
            }catch(Exception e){
                System.err.println("Can't convert " + o.getClass() + " to Node");
                throw new IllegalArgumentException();
            }
    }
    
    @Override
    public String toString(){
        return "profit = " + this.profit + "\t weight = " + this.weight;
    }
}
